// pages/finefood/finefood.js
var app = getApp()
Page({
  data: {
    showLoading: true,
    noMoretip: false,
    page: 0,
  },
  onLoad: function (e) {
    var that = this;
    // if (e && e.title) {
    //   that.setData({
    //     title: e.title
    //   })
    // }
    // var title = that.data.title ? that.data.title :'美食区';
    // app.setNavtitle(title);
    if (!app.globalData.plumSession) {
      //回调
      app.plumSessionCallback = plumSession => {  
          console.log('查询到登录信息');
          that.requestDetail(); 
      }
    } else {
      that.requestDetail();
    }
  },
  onShow: function () {
  },
  requestDetail: function () {
    var that = this;
    //发起请求，获取列表列表
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: {
        map: 'applet_sequence_menu_index'
      },
      success: function (res) {
        if (res.data.ec == 200) {
          console.log(res.data.data)
          var indexInfo = res.data.data;
          var categoryid = that.data.categoryid ? that.data.categoryid:'';
          if (!categoryid){
            if (indexInfo.category && indexInfo.category.length > 0) {
              categoryid = indexInfo.category[0].id
            }
            that.setData({
              categoryid: categoryid
            })
          }
          that.setData({
            indexInfo: indexInfo,
            categoryid: categoryid
          })
          if (indexInfo.title){
            wx.setNavigationBarTitle({
              title: indexInfo.title
            })
          }
          that.requestMenulist();
        } else {
          console.log(res.data)
          app.errorTip(that, res.data.em, 2000);
          that.setData({
            indexInfo: ""
          })
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  requestMenulist: function () {
    var that = this;
    var data = {};
    var page = that.data.page;
    data.map = 'applet_sequence_menu_list';
    data.category = that.data.categoryid ? that.data.categoryid:'';
    data.page = page;
    console.log(data);
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        console.log(page);
        console.log(res.data);
        if (res.data.ec == 200) {
          var allArr = [];
          var initArr = that.data.menuList ? that.data.menuList : [];
          var curArr = res.data.data;
          var lastPageLength = curArr.length;
          if (page > 0) {
            allArr = initArr.concat(curArr);
          } else {
            allArr = res.data.data;
          }
          that.setData({
            menuList: allArr
          })
          if (lastPageLength < 10) {
            that.setData({
              noMoretip: true,
              showLoading: false
            });
          }
          console.log(that.data.menuList);
        } else {
          if (page <= 0) {
            that.setData({
              menuList: [],
              noMoretip: false,
              showLoading: false
            })
          } else {
            that.setData({
              noMoretip: true,
              showLoading: false
            });
          }
        }
      },
      complete: function () {
        wx.stopPullDownRefresh();
      }
    });
  },
  toggleCategory:function(e){
    var that = this;
    var cateid = e.currentTarget.dataset.id;
    that.setData({
      page: 0,
      noMoretip: false,
      showLoading: true,
      categoryid: cateid,
      menuList: null
    })
    that.requestMenulist();
  },
  onPullDownRefresh: function () {
    var that = this;
    that.setData({
      page: 0,
      noMoretip: false,
      showLoading: true
    });
    that.onLoad();
  },
  onReachBottom: function () {
    var that = this;
    console.log("到达页面底部")
    var isMore = that.data.noMoretip;
    var page = that.data.page;
    page++;
    that.setData({
      page: page
    });
    if (isMore) {
      console.log("已完成或正在加载");
    } else {
      that.requestMenulist();
    }
  },
  onPageScroll: function (e) {
    var that = this;
    var scrollTop = e.scrollTop;
    var posttabTop = that.data.posttabTop ? that.data.posttabTop:0;
    if (scrollTop > 480 && that.data.showBacktop != 1) {
      that.setData({ showBacktop: 1 })
    } else if (scrollTop <= 480 && that.data.showBacktop != 0) {
      that.setData({ showBacktop: 0 })
    }
    wx.createSelectorQuery().select('#topbanner').boundingClientRect(function (rect) {
      if (rect && rect.height) {
        posttabTop = rect.height;
      }

      if (scrollTop >= posttabTop && that.data.isFixed != 1) {
        that.setData({ isFixed: 1 })
      } else if (scrollTop < posttabTop && that.data.isFixed != 0) {
        that.setData({ isFixed: 0 })
      }
    }).exec();
  },
  requestIslike: function (e) {
    var that = this;
    var menu = e.currentTarget.dataset.menu;
    var index = e.currentTarget.dataset.index;
    //发起请求，获取列表列表
    wx.request({
      url: app.globalData.requestUrl,
      data: {
        map: 'applet_sequence_menu_like',
        id: menu.id
      },
      success: function (res) {
        if (res.data.ec == 200) {
          console.log(res.data.data)
          if (menu.isLike == 0) {
            if (menu.likeNum < 9999) {
              menu.likeNum++;
            }
            menu.isLike = 1;
          } else {
            if (menu.likeNum < 9999) {
              menu.likeNum--;
            }
            menu.isLike = 0;
          }
          that.setData({
            [`menuList[${index}]`]: menu
          })
          console.log(that.data.menuList);
          wx.showToast({
            title: res.data.data.msg,
            icon: 'none',
            duration: 2000
          })
        } else {
          console.log(res.data)
          wx.showToast({
            title: res.data.em,
            icon: 'none',
            duration: 2000
          })
        }
      },
      complete: function () {
      }
    });
  },
  toCuslink: function (e) {
    var that = this;
    var userInfo = app.globalData.userInfo;
    console.log(userInfo.leaderStatus);
    var url = e.currentTarget.dataset.link;
    if (url && url == '/pages/applyTuan/applyTuan') {
      if (userInfo.leaderStatus == 2) {
        wx.navigateTo({
          url: '/subpages/tuanManage/tuanManage',
        })
      } else {
        wx.navigateTo({
          url: url + '?status=' + userInfo.leaderStatus,
        })
      }
    } else {
      wx.navigateTo({
        url: url,
      })
    }
  },
  todetail:function(e){
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/subpages0/finefoodDetail/finefoodDetail?id=' + id
    })
  },
  stopprop:function(e){
    console.log(e);
  },
  getPoint: function (menu,index) {
    var that = this;
    setTimeout(function () {
      wx.request({
        url: app.globalData.requestUrl,
        data: {
          map: 'applet_share_get_point',
          type: 'menu',
          id: menu.id
        },
        success: function (res) {
          console.log(res.data);
          if (res.data.ec == 200) {
            if (res.data.data.msg) {
              console.log(res.data.data.msg);
              // wx.showModal({
              //   title: '',
              //   content: res.data.data.msg,
              //   showCancel: false
              // })
              if (menu.shareNum < 9999) {
                menu.shareNum++;
              }
              that.setData({
                [`menuList[${index}]`]: menu
              })
            }
          } else {
            if (res.data.em) {
              // wx.showModal({
              //   title: '',
              //   content: res.data.em,
              //   showCancel: false
              // })
            }
          }
        },
        complete: function () {
        }
      });
    }, 3000)
  },
  onShareAppMessage: function (e) {
    console.log(e);
    var that = this;
    var menu = e.target.dataset.menu;
    var index = e.target.dataset.index;
    var mid = app.globalData.userInfo ? app.globalData.userInfo.mid : '';
    var id = menu.id;
    var title = menu && menu.title ? menu.title : '';
    var cover = menu && menu.cover ? menu.cover : '';
    that.getPoint(menu,index);
    return {
      title: title,
      imageUrl: cover,
      path: 'subpages0/finefoodDetail/finefoodDetail?from=share&id=' + id
    }
  }
})